using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Collections;

namespace marbletrack
{
    class FlyThroughCamera
    {
        public bool FlyCamEnabled;

        MouseState originalMouseState;
        KeyboardState originalKeyboardSate;

        public FlyThroughCamera()
        {
            FlyCamEnabled = false;

            Mouse.SetPosition(400, 300);
            originalMouseState = Mouse.GetState();
            originalKeyboardSate = Keyboard.GetState();
        }

        public void Update()
        {

            MouseState mouseState = Mouse.GetState();
            KeyboardState keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.D1) && originalKeyboardSate.IsKeyUp(Keys.D1))
                FlyCamEnabled = !FlyCamEnabled;

            if (FlyCamEnabled)
            {
                //W, S, A, D keyboard controls
                if (keyboardState.IsKeyDown(Keys.A))
                    Game1.view *= Matrix.CreateTranslation(100, 0, 0);
                if (keyboardState.IsKeyDown(Keys.D))
                    Game1.view *= Matrix.CreateTranslation(-100, 0, 0);
                if (keyboardState.IsKeyDown(Keys.S))
                    Game1.view *= Matrix.CreateTranslation(0, 0, -100);
                if (keyboardState.IsKeyDown(Keys.W))
                    Game1.view *= Matrix.CreateTranslation(0, 0, 100);

                //Sets mouse to center of screen
                if (keyboardState.IsKeyDown(Keys.Space) && originalKeyboardSate.IsKeyUp(Keys.Space))
                {
                    Mouse.SetPosition(400, 300);
                    mouseState = Mouse.GetState();
                }

                //Control camera look with the mouse if left mouse button is down
                if (keyboardState.IsKeyDown(Keys.Space))
                {
                    int deltaMouseX = mouseState.X - originalMouseState.X;
                    int deltaMouseY = mouseState.Y - originalMouseState.Y;
                    int deltaScroll = mouseState.ScrollWheelValue - originalMouseState.ScrollWheelValue;

                    Game1.view *= Matrix.CreateFromYawPitchRoll(deltaMouseX * 0.01f, deltaMouseY * 0.01f, deltaScroll * 0.001f);

                    Mouse.SetPosition(400, 300);
                    originalMouseState = Mouse.GetState();
                }
            }

            originalKeyboardSate = keyboardState;
        }
    }
}
